package chunmap;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import chunmap.util.ChunDaoException;

public class Main {

	static String driver = "org.postgresql.Driver";
	static String url = "jdbc:postgresql://localhost:5432/tanke";
	static String userName = "postgres";
	static String passWord = "111";

	/**
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			throw new ChunDaoException(e);
		}
		Connection connection = DriverManager.getConnection(url, userName,
				passWord);
		Statement stmt = connection.createStatement();
		String sql = "insert into user_table values('yjd4','yangjiand','111',23)";
		// stmt.execute("insert into user_table values('yjd2','yangjiand','111',23)");

		int rows = stmt.executeUpdate(sql, autoGenKeyMode(connection));
		Object o = executeResult(stmt, rows);

		System.out.println(o);
		System.out.println(connection.getMetaData().supportsGetGeneratedKeys());
	}

	static int autoGenKeyMode(Connection conn) throws SQLException {
		return conn.getMetaData().supportsGetGeneratedKeys() ? java.sql.Statement.RETURN_GENERATED_KEYS
				: java.sql.Statement.NO_GENERATED_KEYS;
	}

	private static Object executeResult(Statement stmt, int rows) {
		try {
			ResultSet rs = stmt.getGeneratedKeys();
			while (rs.next()) {
				System.out.println(rs);
				int i = rs.getMetaData().getColumnCount();
				System.out.println(rs.getString(1));
				System.out.println(rs.getString(2));
				System.out.println(rs.getString(3));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Long.valueOf(rows);
	}

}
